package com.wsj.message_server.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wsj.message_server.service.SystemIMService;
import com.wsj.message_server.utils.HttpUtils;
import com.wsj.message_server.utils.IMUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class SystemIMServiceImpl implements SystemIMService {

    @Override
    public String addFriend(String From_Account, String To_Account, String Remark, String GroupName, String AddSource, String AddWording, String addType) {

        String url = IMUtils.createFriendAddUrlUrl();
        JSONObject postJson = new JSONObject();
        postJson.put("From_Account", From_Account);
        postJson.put("AddType", addType);

        Map<String,Object> item = new HashMap<>();
        item.put("To_Account", To_Account);
        item.put("Remark", Remark);
        if (!StringUtils.isEmpty(GroupName)) {
            item.put("GroupName", GroupName);
        }
        item.put("AddSource", AddSource);
        item.put("AddWording", AddWording);

        List addFriendItem = new ArrayList();
        addFriendItem.add(item);
        postJson.put("AddFriendItem", addFriendItem);

        String resStr = HttpUtils.doPost(url, postJson.toString(), 4000);
        JSONObject result = JSON.parseObject(resStr);
        JSONArray ResultItem = result.getJSONArray("ResultItem");
        if (ResultItem == null){
            return "添加失败";
        }
        System.out.println(ResultItem);
        JSONObject ob = (JSONObject) ResultItem.get(0);//得到json对象
        String name = ob.getString("ResultCode");//name这里是列名称，获取json对象中列名为name的值
        System.out.print(name);
        return name;
    }

    @Override
    public String deleteFriend(String From_Account, String To_Account, String DeleteType) {
        String url = IMUtils.createFiendDelUrl();
        JSONObject postJson = new JSONObject();
        postJson.put("From_Account", From_Account);
        postJson.put("DeleteType", DeleteType);
        List accountList = new ArrayList();
        accountList.add(To_Account);
        postJson.put("To_Account", accountList);
        String resStr = HttpUtils.doPost(url, postJson.toString(), 4000);
        JSONObject result = JSON.parseObject(resStr);

        JSONArray ResultItem = result.getJSONArray("ResultItem");
        if (ResultItem == null){
            return "删除失败";
        }
        System.out.println(ResultItem);
        JSONObject ob = (JSONObject) ResultItem.get(0);//得到json对象
        String name = ob.getString("ResultCode");//name这里是列名称，获取json对象中列名为name的值
        System.out.print(name);
        return name;
    }

    @Override
    public String updateFriend(String From_Account, String To_Account, String Tag, Object Value) {
        String url = IMUtils.createFiendUpdateUrl();
        JSONObject postJson = new JSONObject();
        postJson.put("From_Account", From_Account);
        Map<String,Object> item = new HashMap<>();
        Map<String,Object> item1 = new HashMap<>();
        item1.put("Tag",Tag);
        item1.put("Value",Value);
        List SnsItem = new ArrayList();
        SnsItem.add(item1);
        item.put("To_Account",To_Account);
        item.put("SnsItem",SnsItem);
        List UpdateItem = new ArrayList();
        UpdateItem.add(item);
        postJson.put("UpdateItem", UpdateItem);
        String resStr = HttpUtils.doPost(url, postJson.toString(), 4000);
        JSONObject result = JSON.parseObject(resStr);

        JSONArray ResultItem = result.getJSONArray("ResultItem");
        if (ResultItem == null){
            return "删除失败";
        }
        System.out.println(ResultItem);
        JSONObject ob = (JSONObject) ResultItem.get(0);//得到json对象
        String name = ob.getString("ResultCode");//name这里是列名称，获取json对象中列名为name的值
        System.out.print(name);
        return name;
    }

    @Override
    public String blackListAdd(String From_Account, String To_Account) {
        String url = IMUtils.blackListAdd();
        JSONObject postJson = new JSONObject();
        postJson.put("From_Account", From_Account);
        List list = new ArrayList();
        list.add(To_Account);
        postJson.put("To_Account", list);
        String resStr = HttpUtils.doPost(url, postJson.toString(), 4000);
        JSONObject result = JSON.parseObject(resStr);

        JSONArray ResultItem = result.getJSONArray("ResultItem");
        if (ResultItem == null){
            return "添加失败";
        }
        System.out.println(ResultItem);
        JSONObject ob = (JSONObject) ResultItem.get(0);//得到json对象
        String name = ob.getString("ResultCode");//name这里是列名称，获取json对象中列名为name的值
        System.out.print(name);
        return name;

    }

    @Override
    public String blackListDelete(String From_Account, String To_Account) {
        String url = IMUtils.blackListDelete();
        JSONObject postJson = new JSONObject();
        postJson.put("From_Account", From_Account);
        List list = new ArrayList();
        list.add(To_Account);
        postJson.put("To_Account", list);
        String resStr = HttpUtils.doPost(url, postJson.toString(), 4000);
        JSONObject result = JSON.parseObject(resStr);

        JSONArray ResultItem = result.getJSONArray("ResultItem");
        if (ResultItem == null){
            return "删除失败";
        }
        System.out.println(ResultItem);
        JSONObject ob = (JSONObject) ResultItem.get(0);//得到json对象
        String name = ob.getString("ResultCode");//name这里是列名称，获取json对象中列名为name的值
        System.out.print(name);
        return name;
    }

    @Override
    public String deleteFriendAll(String from_account, String deleteType) {
        String url = IMUtils.createFiendDelAllUrl();
        JSONObject postJson = new JSONObject();
        postJson.put("From_Account", from_account);
        postJson.put("DeleteType", deleteType);
        String resStr = HttpUtils.doPost(url, postJson.toString(), 4000);
        JSONObject result = JSON.parseObject(resStr);

        JSONArray ResultItem = result.getJSONArray("ResultItem");
        if (ResultItem == null){
            return "删除失败";
        }
        System.out.println(ResultItem);
        JSONObject ob = (JSONObject) ResultItem.get(0);//得到json对象
        String name = ob.getString("ResultCode");//name这里是列名称，获取json对象中列名为name的值
        System.out.print(name);
        return name;
    }


}
